﻿using Barcode_Project.Entity;
using Barcode_Project.Network;
using DevExpress.XtraEditors;
using log4net;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Drawing.Printing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.UI.WebControls;

namespace Barcode_Project.Local
{
    // 本地重复处理任务
    public class ComputerTask
    {
        ILog _log;
        public ComputerTask()
        {
            _log = LogManager.GetLogger(this.GetType().Name);
        }
        // 获取仓库列表
        public void LoadWarehouse(LookUpEdit lookup)
        {
            var list = new RemoteRequest().GetWarehouseList();

            lookup.Properties.DataSource = list;
            lookup.Properties.DisplayMember = "name";
            lookup.Properties.ValueMember = "code";
        }
        // 获取机台列表
        public void LoadMachine(LookUpEdit lookup)
        {
            var list = new RemoteRequest().GetMachineList();

            lookup.Properties.DataSource = list;
            lookup.Properties.DisplayMember = "name";
            lookup.Properties.ValueMember = "code";
        }
        // 获取打印机列表
        public void LoadPrinter(LookUpEdit lookup)
        {
            var list = new List<ItemSelector>();
            foreach (string printName in PrinterSettings.InstalledPrinters)
            {
                list.Add(new ItemSelector { code = printName, name = printName });
            }

            lookup.Properties.DataSource = list;
            lookup.Properties.DisplayMember = "name";
            lookup.Properties.ValueMember = "code";
        }
        // 获取打印模板列表
        public void LoadTemplate(LookUpEdit lookup)
        {
            var list = new List<ItemSelector>();

            try
            {
                string path = Path.Combine(
                ConfigurationManager.AppSettings["program_path"].ToString(),
                @"Templates\");

                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }

                var files = Directory.GetFiles(path, "*.mb");

                foreach (var filePath in files)
                {
                    var fileName = Path.GetFileName(filePath);
                    list.Add(new ItemSelector { code = filePath, name = fileName });
                }

                lookup.Properties.DataSource = list;
                lookup.Properties.DisplayMember = "name";
                lookup.Properties.ValueMember = "code";
            }
            catch (Exception ex)
            {
                _log.Error("获取打印模板失败：");
                _log.Error(ex.Message);
            }

        }
        // 向打印机发送打印任务
        public bool StartPrintingTask(DataTable dt, string printerName, string templatePath, bool preview = false)
        {
            var result = false;

            try
            {
                var easyPrint = new VBprinter.EasyReport();


                easyPrint.SetReportDataSource(dt);
                easyPrint.SetReportFile(templatePath);

                result = easyPrint.PrintReport(false, !preview, false, false, printerName);
            }
            catch (Exception ex)
            {
                _log.Error(ex.Message);
            }
            return result;
        }
    }
}
